package com.hk.rabbitmq.five;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.apache.commons.io.FileUtils;

import java.io.File;

/**
 * @author: dpb
 * @date: 2022/3/22
 */
public class ReceiveLogs02 {

    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        String queueName = channel.queueDeclare().getQueue();
        //绑定
        channel.queueBind(queueName, EXCHANGE_NAME, "789");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), "UTF-8");
            File file = new File("D:\\msg.txt");
            msg = "接收绑定主键为：" + message.getEnvelope().getRoutingKey() + "，消息为：" + msg;
            FileUtils.writeStringToFile(file, msg, "UTF-8");
            System.out.println("数据写入成功");
        };
        channel.basicConsume(queueName, true, deliverCallback, CancelCallback -> {
        });
    }
}
